home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir40 / pm_10_94.zip / EXAMPLE.ASM next >
Assembly Source File  |  1994-09-09  |  7KB  |  203 lines

  1. ; This little program just prints a string to DOS using function 0300h.
  2. ; Then it allocates a real mode callback, calls the real mode callback from
  3. ; protected mode, and prints a string out of the protected mode callback
  4. ; routine. Not the standard usage of callbacks, but just to demonstrate.
  5.  
  6. .386p
  7. locals
  8.  
  9. STACKLEN        = 40h                   ; size of stack in paragraphs
  10.  
  11. PMODE_TEXT      segment para public use16 'CODE'
  12. PMODE_TEXT      ends
  13. _TEXT           segment para public use16 'CODE'
  14. _TEXT           ends
  15. EXE_STACK       segment para stack use16 'STACK'
  16. EXE_STACK       ends
  17.  
  18. extrn   _pm_info:far, _pm_init:far
  19.  
  20. _TEXT           segment para public use16 'CODE'
  21. assume  cs:_TEXT, ds:_TEXT
  22. org     0
  23.  
  24. ;═════════════════════════════════════════════════════════════════════════════
  25. errmsgtbl       dw      errmsg0,errmsg1,errmsg2,errmsg3
  26.                 dw      errmsg4,errmsg5,errmsg6
  27.  
  28. errmsg0         db      'Not enough low memory!',13,10,36
  29. errmsg1         db      '80386 or better not detected!',13,10,36
  30. errmsg2         db      'System already in protected mode and no VCPI or DPMI found!',13,10,36
  31. errmsg3         db      'DPMI host is not 32bit!',13,10,36
  32. errmsg4         db      'Could not enable A20 gate!',13,10,36
  33. errmsg5         db      'Could not enter DPMI 32bit protected mode!',13,10,36
  34. errmsg6         db      'Could not allocate needed DPMI selectors!',13,10,36
  35.  
  36. msg0            db      'Hello World from Protected Mode...',13,10
  37.                 db      'Press any key to continue...',13,10,36
  38. msg1            db      'Hello World from a Real Mode Callback...',13,10
  39.                 db      'Press any key to exit...',13,10,36
  40.  
  41. align 4
  42. regstruc_edi    label   dword
  43. regstruc_di     dw      ?,?
  44. regstruc_esi    label   dword
  45. regstruc_si     dw      ?,?
  46. regstruc_ebp    label   dword
  47. regstruc_bp     dw      ?,?
  48.                 dd      ?
  49. regstruc_ebx    label   dword
  50. regstruc_bx     label   word
  51. regstruc_bl     db      ?
  52. regstruc_bh     db      ?,?,?
  53. regstruc_edx    label   dword
  54. regstruc_dx     label   word
  55. regstruc_dl     db      ?
  56. regstruc_dh     db      ?,?,?
  57. regstruc_ecx    label   dword
  58. regstruc_cx     label   word
  59. regstruc_cl     db      ?
  60. regstruc_ch     db      ?,?,?
  61. regstruc_eax    label   dword
  62. regstruc_ax     label   word
  63. regstruc_al     db      ?
  64. regstruc_ah     db      ?,?,?
  65. regstruc_flags  dw      ?
  66. regstruc_es     dw      ?
  67. regstruc_ds     dw      ?
  68. regstruc_fs     dw      ?
  69. regstruc_gs     dw      ?
  70. regstruc_ip     dw      ?
  71. regstruc_cs     dw      ?
  72. regstruc_sp     dw      ?
  73. regstruc_ss     dw      ?
  74.  
  75. dataselector    dw      ?
  76.  
  77. ;═════════════════════════════════════════════════════════════════════════════
  78. start:                                  ; execution starts here
  79.         push cs                         ; DS = CS
  80.         pop ds
  81.  
  82.         call _pm_info                   ; get information
  83.         jnc short @@startf0             ; if no error, go on
  84.  
  85. @@startf1:
  86.     mov si,ax            ; print error message for code AX
  87.         add si,ax
  88.         mov dx,errmsgtbl[si]
  89.         mov ah,9
  90.         int 21h
  91.         mov ax,4cffh
  92.         int 21h
  93.  
  94. @@startf0:
  95.     xor ax,ax            ; check low memory and allocate low
  96.     mov cx,ss            ;  buffer needed for protected mode
  97.         add cx,STACKLEN
  98.         mov dx,es:[2]
  99.         sub dx,cx
  100.         cmp dx,bx
  101.         jb @@startf1
  102.         mov es,cx
  103.  
  104.     call _pm_init            ; enter protected mode
  105.     jc @@startf1            ; if error, go to error message
  106.  
  107.         mov dataselector,ds             ; now in protected mode, preserve data
  108.                                         ;  selector
  109.  
  110.         push ds                         ; ES = DS for register structure
  111.         pop es
  112.  
  113.         mov edi,offset regstruc_edi     ; offset of register structure
  114.         xor cx,cx                       ; no parameters on stack
  115.         mov bx,21h                      ; call interrupt 21h
  116.         mov ax,300h                     ; INT 31h function 0300h
  117.  
  118.         mov regstruc_ah,9               ; function code 9, put string
  119.         mov regstruc_ds,_TEXT           ; set DS:DX for DOS string put
  120.         mov regstruc_dx,offset msg0
  121.         mov regstruc_ss,0               ; SS:SP = 0, PMODE will provide stack
  122.         mov regstruc_sp,0
  123.  
  124.         int 31h                         ; do the call to real mode
  125.  
  126.  
  127.         xor ah,ah                       ; wait for keypress
  128.         int 16h
  129.  
  130.  
  131.         push ss                         ; ES = SS for register structure
  132.         pop es
  133.  
  134.         sub esp,32                      ; stack space for register structure
  135.         mov edi,esp                     ; EDI = offset of register structure
  136.  
  137.         push cs                         ; DS:ESI = callback routine
  138.         pop ds
  139.         mov esi,offset callback
  140.  
  141.         mov ax,303h                     ; INT 31h function 0303h
  142.         int 31h                         ; allocate real mode callback
  143.  
  144.  
  145.         sub esp,32                      ; stack space for register structure
  146.         mov edi,esp                     ; EDI = offset of register structure
  147.  
  148.         mov dword ptr [esp+2eh],0       ; SS:SP = 0 in register structure
  149.         mov [esp+2ah],dx                ; IP is offset of real mode callback
  150.         mov [esp+2ch],cx                ; CS is segment of real mode callback
  151.         mov word ptr [esp+20h],0        ; zero FLAGS in register structure
  152.  
  153.         xor cx,cx                       ; no parameters on stack
  154.         xor bh,bh                       ; BH must be 0
  155.         mov ax,301h                     ; INT 31h function 0301h
  156.  
  157.         int 31h                         ; call real mode callback
  158.  
  159.  
  160.         add esp,64                      ; free stack space
  161.  
  162.         mov ah,4ch                      ; exit to DOS
  163.         int 21h
  164.  
  165. ;─────────────────────────────────────────────────────────────────────────────
  166. callback:
  167.         cld
  168.         lods dword ptr ds:[esi]         ; get return seg:off from real mode
  169.         mov dword ptr es:[edi+2ah],eax  ;  stack, and update return address
  170.         add word ptr es:[edi+2eh],4     ; adjust SP in register structure
  171.  
  172.         push es edi                     ; preserve register structure ES:EDI
  173.  
  174.  
  175.         mov es,cs:dataselector          ; ES = main stream data selector
  176.  
  177.         mov edi,offset regstruc_edi     ; offset of register structure
  178.         xor cx,cx                       ; no parameters on stack
  179.         mov bx,21h                      ; call interrupt 21h
  180.         mov ax,300h                     ; INT 31h function 0300h
  181.  
  182.         mov es:regstruc_dx,offset msg1  ; offset of new string, other fields
  183.                                         ;  were set previously
  184.         int 31h                         ; do the call to real mode
  185.  
  186.  
  187.         xor ah,ah                       ; wait for keypress
  188.         int 16h
  189.  
  190.  
  191.         pop edi es                      ; restore register structure ES:EDI
  192.  
  193.         iretd                           ; done with callback
  194.  
  195. _TEXT           ends
  196.  
  197. EXE_STACK       segment para stack use16 'STACK'
  198.                 db      STACKLEN*10h dup(?)
  199. EXE_STACK       ends
  200.  
  201. end     start
  202.  
  203.